* Open SCAD
use "scad.dta", clear
desc
count

* append latin american data
append using "scad_lar.dta"

* start in 1990 and end in 2010
drop if styr < 1990
drop if styr > 2010
count

* fix negative event ids
replace eventid = abs(eventid)
summ eventid

sort ccode
tab ccode

* drop location info
drop if sublocal != 1
count

********************
* year-level dataset
********************
preserve

keep ccode countryname styr
destring styr, replace
ren styr year
sort ccode year

gen count = 1
collapse (sum) count, by(ccode countryname year)

rename count numb_allevents
label variable numb_allevents "All Events"


save "scad_year_4merge.dta", replace
count
desc

restore


***************************
* year-month level dataset
***************************

preserve

keep ccode countryname styr stmo
destring styr, replace
ren styr year
destring stmo, replace
ren stmo month

order ccode year month
sort ccode year month

gen count = 1
collapse (sum) count, by(ccode countryname year month)

rename count numb_allevents
label variable numb_allevents "All Events"


save "scad_month_4merge.dta", replace
count
desc

restore

*************
* event date
*************
desc styr stmo stday
summ styr stmo stday
egen stdate = concat(stmo stday styr), punct(/)
gen event_sdate = date(stdate,"MDY")
* br stmo stday styr stdate event_sdate

desc eyr emo eday
summ eyr emo eday
egen edate = concat(emo eday eyr), punct(/)
gen event_edate = date(edate,"MDY")
* br emo eday eyr edate event_edate

* note that the event date may go over 31 Dec 2010
summ event_sdate event_edate

expand duration
bysort eventid: gen day = event_sdate + _n - 1

gen count = 1
gen count_progovt = 0
replace count_progovt = 1 if etype == 7
gen count_progovt1 = 0
replace count_progovt1 = 1 if repress == 1 | repress == 2
gen count_antigovt = 0
replace count_antigovt = 1 if etype == 8
gen count_antigovt1 = 0
replace count_antigovt1 = 1 if cgovtarget == 1
gen count_antigovt2 = 0
replace count_antigovt2 = 1 if cgovtarget == 1 & (issue1 == 1 | issue2 == 1 | issue3 == 1)

collapse (sum) count count_progovt* count_antigovt*, by(ccode countryname day)
count


******************
* Label variables
******************

rename count numb_allevents
rename count_progovt numb_progovt
rename count_progovt1 numb_progovt1
rename count_antigovt numb_antigovt
rename count_antigovt1 numb_antigovt1
rename count_antigovt2 numb_antigovt2

label variable numb_allevents "All Events"
label variable numb_progovt "Pro-Govt Violence"
label variable numb_progovt1 "Repression Used"
label variable numb_antigovt "Anti-Govt Violence"
label variable numb_antigovt1 "Central Govt Target"
label variable numb_antigovt2 "Central Govt Target & Election Issue"

******************
* save file
******************
save "scad_4merge.dta", replace
count
desc
